//
//  StatusOriginalView.m
//  CMJWeibo
//
//  Created by BH on 16/1/4.
//
//

#import "StatusOriginalView.h"
#import "StatusFrame.h"
#import "PhotosView.h"
#import "CMJCommon.h"
#import "StatusModel.h"
#import "UserInfo.h"
#import "UIImage+MJ.h"
#import "UIView+MJ.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface StatusOriginalView ()

/** 头像 */
@property (nonatomic, strong) UIImageView *iconView;
/** 会员图标 */
@property (nonatomic, strong) UIImageView *vipView;
/** 昵称 */
@property (nonatomic, strong) UILabel *nickName;
/** 时间 */
@property (nonatomic, strong) UILabel *time;
/** 来源 */
@property (nonatomic, strong) UILabel *source;
/** 正文\内容 */
@property (nonatomic, strong) UILabel *content;
/** 配图(相册) */
@property (nonatomic, weak) PhotosView *photosView;

@end

@implementation StatusOriginalView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //头像
        UIImageView *iconImg = [[UIImageView alloc] init];
        [self addSubview:iconImg];
        self.iconView = iconImg;
        
        //会员图标
        UIImageView *vipImg = [[UIImageView alloc] init];
        vipImg.contentMode = UIViewContentModeCenter;
        [self addSubview:vipImg];
        self.vipView = vipImg;
        
        //昵称
        UILabel *nickNameLab = [[UILabel alloc] init];
        nickNameLab.font = NicknameFont;
        [self addSubview:nickNameLab];
        self.nickName = nickNameLab;
        
        //时间
        UILabel *timeLab = [[UILabel alloc] init];
        timeLab.textColor = [UIColor orangeColor];
        timeLab.font = TimeFont;
        [self addSubview:timeLab];
        self.time = timeLab;
        
        //来源
        UILabel *sourceLab = [[UILabel alloc] init];
        sourceLab.font = TimeFont;
        sourceLab.textColor = [UIColor grayColor];
        [self addSubview:sourceLab];
        self.source = sourceLab;
        
        //正文
        UILabel *contentLab = [[UILabel alloc] init];
        contentLab.font = ContentFont;
        contentLab.numberOfLines = 0;
        [self addSubview:contentLab];
        self.content = contentLab;
        
        //配图
        PhotosView *photosV = [[PhotosView alloc] init];
        [self addSubview:photosV];
        self.photosView = photosV;
    }
    return self;
}

-(void)setStatusF:(StatusFrame *)statusF
{
    _statusF = statusF;
    
    // 设置数据
    [self setupData];
    
    // 设置frame
    [self setupFrame];
}

/**
 * 设置数据
 */
-(void)setupData
{
    StatusModel *status = self.statusF.status;
    UserInfo *user = status.user;
    
    //头像
    NSURL *iconUrl = [NSURL URLWithString:user.profile_image_url];
    [self.iconView sd_setImageWithURL:iconUrl placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
    
    //是否会员
    if ([user isVip]) {
        //是会员
        NSString *imageName = [NSString stringWithFormat:@"common_icon_membership_level%ld",user.mbrank];
        self.vipView.image = [UIImage imageWithName:imageName];
        self.nickName.textColor = [UIColor orangeColor];
        self.vipView.hidden = NO;
    }else{
        //不是会员
        self.vipView.hidden = YES;
        self.nickName.textColor = [UIColor blackColor];
    }
    
    //昵称
    self.nickName.text = user.name;
    
    //时间
    self.time.text = status.created_at;
    
    //来源
    self.source.text = status.source;
    
    //正文
    self.content.text = status.text;
    
    //配图
    self.photosView.photosArr = status.pic_urls;
}

/**
 * 设置frame
 */
-(void)setupFrame
{
    self.frame = self.statusF.originalViewF;
    
    //头像
    self.iconView.frame = self.statusF.originalIconF;
    
    //会员图标
    self.vipView.frame = self.statusF.originalVipF;
    
    //昵称
    self.nickName.frame = self.statusF.originalNameF;
    
    //时间
    self.time.x = self.nickName.x;
    self.time.y = CGRectGetMaxY(self.nickName.frame)+CellPadding*0.5;
    NSString *timeStr = self.statusF.status.created_at;
    self.time.size = [timeStr sizeWithFont:TimeFont];
    
    //来源
    self.source.x = CGRectGetMaxX(self.time.frame)+CellPadding;
    self.source.y = self.time.y;
    NSString *sourceStr = self.statusF.status.source;
    self.source.size = [sourceStr sizeWithFont:TimeFont];
    
    //正文
    self.content.frame = self.statusF.originalContentF;
    
    //配图
    self.photosView.frame = self.statusF.originalPhotosF;
}

@end
